﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MapPageSiteMaster.Mobile.Master"
    Inherits="System.Web.Mvc.ViewPage<Micrhard.FMCG.Server.Models.CheckinModel>" %>

<%@ Import Namespace="Micrhard.FMCG.Server.Extension" %>
<%@ Import Namespace="Microhard.Framework.MVCHelper" %>
<%@ Import Namespace="Microhard.Permission.Extension" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div data-role="navbar">
        <ul>
            <li><a href="#" class="ui-btn-active ui-state-persist">签到</a></li>
        </ul>
    </div>
    <%=Html.ValidationSummary(true)%>
    <%=Html.ValidationMessage("SaveFailure")%>
    <%using (Html.BeginForm("SaveCreate", "CheckIn", FormMethod.Post, new { id = "groupform" }))
      {
    %>
    <%=Html.Hidden("WeixinToken", ViewData["WeixinToken"])%>
    <%=Html.Hidden("WeixinName", ViewData["WeixinName"])%>
    <%=Html.Hidden("CurrentLoginUserName", Html.GetCurrentUserInfoFromSession().UserName)%>
    <%=Html.HiddenFor(m => m.Baidulng)%>
    <%=Html.HiddenFor(m => m.Baidulat)%>
    <fieldset data-role="controlgroup" id="SubKAIDForSel">
        <legend>签到门店加载中......</legend>
    </fieldset>
    <div data-role="fieldcontain" class="ui-hide-label" id="divAddress">
        <label>
            地址</label>
        <%=Html.EditorFor(m=>m.Address) %>
        <%=Html.ValidationMessageFor(m=>m.Address) %>
        <%=Html.ValidationMessageFor(m=>m.Baidulat) %>
        <%=Html.ValidationMessageFor(m=>m.Baidulng) %>
    </div>
    <div data-role="fieldcontain" class="ui-hide-label" style="display: none" id="divError">
        <label>
            如果您在签到门店附近，但是刷新还是未获得签到信息，请直接点击提交按钮，获得陈列码进行签到</label>
        <%=Html.HiddenFor(m=>m.Error) %>
        <%=Html.ValidationMessageFor(m=>m.Error) %>
    </div>
    <input type="submit" value="提交" id="btnSubmit" name="btnSubmit" data-theme="b" />
    <a href="<%=ViewData["fullUrl"]%>" target="_self" data-role="button">刷新</a>
    <%} %>
    <script type="text/javascript">
        $(document).ready(function () {
            var mySel = $('#SubKAIDForSel');
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function (r) {
                if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                    $('#Baidulng').val(r.point.lng);
                    $('#Baidulat').val(r.point.lat);
                    var gc = new BMap.Geocoder();
                    gc.getLocation(r.point, function (rs) {
                        var addComp = rs.addressComponents;
                        var address = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
                        $('#Address').val(address);
                        var pointA = r.point;
                        if (mngSubKas.length > 0) {
                            mySel.empty();
                            var map = new BMap.Map();
                            var ck = '';
                            for (var i = 0; i < mngSubKas.length; i++) {
                                //获得门店与当前人的距离                      
                                var pointB = new BMap.Point(mngSubKas[i].Lng, mngSubKas[i].Lat);
                                var distance = map.getDistance(pointA, pointB);                                                                 
                                if (distance <= scope) {
                                    if(i == 0)
                                    {
                                        ck = 'checked="checked"';
                                    }
                                    else
                                    {
                                        ck = '';
                                    }
                                    var item = '<input class="selectedlocation" data-mini="true" name="SubKAsForCheckIn" id="SubKAsForCheckIn_' + i + '" type="radio" '+ ck +'  value="' + mngSubKas[i].SubID + '">';
                                    mySel.append(item);
                                    var disp = '<label for="SubKAsForCheckIn_' + i + '">门店名称:' + mngSubKas[i].Name + ' (距离:' + Math.ceil(distance) + '米)</label>';
                                    mySel.append(disp);
                                }
                                else
                                {
                                     mySel.html("暂时无法获取附件的门店信息，可能距离太远，请重新刷新");
                                }
                            }
                        }
                    });
                }
                else {
                    mySel.html("获取签到门店失败,错误:"+ this.getStatus() +"，请刷新重试");
                    var divError = $("#divError");
                    divError.show();
                    var divAddress = $("#divAddress");
                    divAddress.hide();
                    var txtError = $("#Error");
                    txtError.val(this.getStatus());
                }
            }, { enableHighAccuracy: true })
        });
        <%var db = ViewData["MngSubKA"] as List<Micrhard.FMCG.Server.Models.SubKAJSONModel>;%>
        var mngSubKas = <%=db.ToArray().ToJSONString() %>;
        var scope = <%=System.Configuration.ConfigurationManager.AppSettings["distanceScope"] %>;
    </script>
</asp:Content>
